package com.ctrip.ec.dal;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by jiangzy on 2018/9/5.
 */
public class TreeDao<Entity extends EntityBase> extends DaoBase<Entity> {
    public TreeDao(Class<Entity> clazz) {
        super(clazz);
    }

    public TreeDao(Class<Entity> clazz, DbAccess db) {
        super(clazz, db);
    }

    @Override
    public void delete(List<Long> ids) throws SQLException {
        for (Long id : ids) {
            //Entity entity = this.entity(id);
            String sql = "delete from " + this.getTableName() + " where path like '%," + id + ",%' or id = " + id;
            this.db.execute(sql);
        }
    }

    public boolean existName(String name, Long parentId, Long id) throws SQLException {
        String sql = "select count(*) from " + this.getTableName() + " where name=@0 and parent_id=@1";
        if(id != null){
            sql += " and id != " + id;
        }
        Long count = this.db.getScalar(Long.class,sql,name,parentId);
        return count>0;
    }

}
